//
//  SingerViewController.m
//  Temp
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 不二晶. All rights reserved.
//

#import "SingerViewController.h"
#import "musicViewController.h"
#import "NetWorkingTool.h"
#import "SingerModel.h"
#import "UIImageView+WebCache.h"
#import "SingerCell.h"
#import "SingerShowViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface SingerViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property(nonatomic, retain)NSMutableArray *singerArr;
@property(nonatomic, retain)UICollectionView *singerCollectionView;
@property(nonatomic, retain)UIActivityIndicatorView *activity;
@end
@implementation SingerViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.singerArr = [NSMutableArray array];
        self.title = @"歌手";
    }
    [self singerData];
    return self;
}
- (void)dealloc
{
    [_singerArr release];
    [_singerCollectionView release];
    [_activity release];
    [super dealloc];
}
// 解析数据
- (void)singerData{
    [NetWorkingTool netWorkingToolStr:@"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=46&app=ttpod&v=v7.7.0.2015012818&uid=&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=8.0.2&active=1&net=2&openudid=50a40f05044a0e2462e11cbf7d6612844b6c0008&idfa=058C286B-AB2C-41E2-A7FE-B67B3496FBBC&utdid=VQKI/UR603ADANyHaLs5uRrS&alf=201200" block:^(id result) {
        NSMutableDictionary *dic = result;
        NSMutableArray *arr = [NSMutableArray array];
        arr = dic[@"data"];
        for (NSInteger i = 0; i < arr.count; i++) {
            SingerModel *singerModel = [[SingerModel alloc] init];
            [singerModel setValuesForKeysWithDictionary:arr[i]];
            [self.singerArr addObject:singerModel];
            [singerModel release];
        }
        [self.singerCollectionView reloadData];
        [self.activity stopAnimating];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"39"]];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 设置item的大小
    flowLayout.itemSize = CGSizeMake(WIDTH/3.75, WIDTH/3.125);
    // 设置最小的行间距
    flowLayout.minimumLineSpacing = 10;
    // 设置最小的列间距
    flowLayout.minimumInteritemSpacing = 1;
    
    // 设置item边框上下左右间距大小
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    
    self.singerCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49) collectionViewLayout:flowLayout];
//    self.singerCollectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"39.jpg"]];
    self.singerCollectionView.delegate = self;
    self.singerCollectionView.dataSource = self;
    
    self.singerCollectionView.backgroundColor = [UIColor clearColor];
    
    [self.view addSubview:self.singerCollectionView];
    
#pragma mark collectionView只能采用注册的方式生成cell
    [self.singerCollectionView registerClass:[SingerCell class] forCellWithReuseIdentifier:@"reuse"];
    
    // 进度轮
    self.activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [self.activity setCenter:CGPointMake(self.view.frame.size.width / 2, (self.view.frame.size.height - 113) / 2)]; // 指定进度轮中心点
    [self.activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:self.activity];
    [self.activity startAnimating];
    [_activity release];
 

    //添加左侧按钮, 实现返回
//    UIImage *image = [UIImage imageNamed:@"01"];
//    // 创建左button
//    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonClick:)] autorelease];
}
// 左侧按钮方法实现
//- (void)leftButtonClick:(UIButton *)bu{
//    musicViewController *musicVC = [[musicViewController alloc] init];
//    [self.navigationController pushViewController:musicVC animated:YES];
//    musicVC.navigationController.navigationBarHidden = YES;
//    [musicVC release];
//}


#pragma mark 协议需要实现的方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.singerArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SingerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    SingerModel *singerModel = [[SingerModel alloc] init];
    singerModel = self.singerArr[indexPath.row];
    cell.myLabel.text = singerModel.title;
    NSURL *url = [NSURL URLWithString:singerModel.pic_url];
    [cell.myImageView sd_setImageWithURL:url];
    
    // 设置代理人
//    cell.delegate = self;
    
    return cell;
}
// 点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    SingerShowViewController *singerShowVC = [[SingerShowViewController alloc] init];
    singerShowVC.passTitle = [self.singerArr[indexPath.row] title];
    singerShowVC.passId = [self.singerArr[indexPath.row] id];
    [self.navigationController pushViewController:singerShowVC animated:YES];
    [singerShowVC release];
}


// 协议实现方法
//- (void)clickThisCell:(UIImageView *)myImgView {
//    CATransition *transition = [CATransition animation];
//    transition.type = @"rippleEffect";
//    [transition setDuration:3];
//    [myImgView.layer addAnimation:transition forKey:@"transition"];
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
